package com.xb.p43_gesturedemo;

import android.content.Context;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

public class ScaleImageView extends AppCompatImageView implements ScaleGestureDetector.OnScaleGestureListener, View.OnTouchListener {
    ScaleGestureDetector mDetector;

    //图片的缩放是基于矩阵来进行的
    Matrix mScaleMatrix=new Matrix();

    //初始化的时候的放大尺寸
    float initScale=1.0f;
    //最大的放大倍数
    static final float SCALE_MAX=6.0f;
    //缩放数组，保存每个坐标点放大尺度
    float[] matrixValues=new float[9];


    public ScaleImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //添加缩放手势监听器
        mDetector=new ScaleGestureDetector(context,this);
        //如果在界面中没有指定处理缩放的方式，则需要在这里进行配置
        //super.setScaleType(ScaleType.MATRIX);
        this.setOnTouchListener(this);

    }

    //获取缩放比例
    private float getScale(){
        mScaleMatrix.getValues(matrixValues);
        return matrixValues[Matrix.MSCALE_X];
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        //获得当前的缩放尺度
        float scale=getScale();
        float scaleFactor=detector.getScaleFactor();
        //判断动作是否超出了缩放边界

        if( (scaleFactor>1.0f && scaleFactor<SCALE_MAX) || (scaleFactor>initScale && scaleFactor<1.0f) ){
            if(scaleFactor * scale <initScale){
                scaleFactor=initScale/scale;
            }

            if(scaleFactor * scale > SCALE_MAX){
                scaleFactor=SCALE_MAX/scale;
            }
        }

        //整个图片基于中心进行放大
        //mScaleMatrix.postScale(scaleFactor,scaleFactor,getWidth()/2,getHeight()/2);

        //基于两个手指的中心进行放大
        mScaleMatrix.postScale(scaleFactor,scaleFactor,detector.getFocusX()/2,detector.getFocusY()/2);

        //检查边界和中心
        checkBorderAndCenterWhenScale();

        setImageMatrix(mScaleMatrix);
        return true;
    }

    private void checkBorderAndCenterWhenScale() {
        //获得图片的绘制区域
        RectF rectF=getMatrixRectF();

        //判断是不是在绘图区域
        float deltaX=0;
        float deltaY=0;

        int width=getWidth();
        int height=getHeight();

        //检测图片的上下左右是否留白
        if(rectF.width()>=width){
            if(rectF.left>0){
                deltaX= -rectF.left;
            }
            if(rectF.right < width){
                deltaX= width -rectF.right;
            }
        }

        if(rectF.height()>=height){
            if(rectF.top>0){
                deltaX= -rectF.top;
            }
            if(rectF.bottom < height){
                deltaX= height -rectF.bottom;
            }
        }

        //图片较小时，居中显示一下
        if(rectF.width() <width){
            deltaX=width*0.5f -rectF.right + 0.5f*rectF.width();
        }
        if(rectF.height() <height){
            deltaY=height*0.5f -rectF.bottom + 0.5f*rectF.height();
        }

        mScaleMatrix.postTranslate(deltaX,deltaY);

    }

    private RectF getMatrixRectF() {
        Matrix matrix=mScaleMatrix;
        RectF rectF=new RectF();

        //可以绘制的对象
        Drawable drawable=getDrawable();
        if(drawable !=null){
            rectF.set(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
            matrix.mapRect(rectF);
        }
        return rectF;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        return true;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {

    }




    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //有触摸事件时，将控制权交给手势识别监听器
        mDetector.onTouchEvent(event);
        return true;
    }
}
